package com.LA_houseLease.app.custom.config;

import com.LA_houseLease.app.custom.interceptor.AuthenticationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * 向SpringMVC注册自定义配置类
 */
@Configuration
@Slf4j
/**
 * WebMvcConfigurer这个接口是由springmvc专门提供的一个用于自定义配置的接口，
 * 只要我们实现了这个接口，就可以去完成各项与springmvc相关的配置
 * （比如注册自定义converter以及注册为自定义拦截器等等等）
 */
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    /**
     * 注册身份校验拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.warn("开始注册身份校验拦截器.............");
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/app/**")
                .excludePathPatterns("/app/login/**");
    }

    // todo 跨域
    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.warn("开始CORS跨域配置加载.............");
        registry.addMapping("/**") //所有接口
                .allowCredentials(true) //是否发送cookie
                .allowedOriginPatterns("*")
                .allowedMethods(new String[]{"GET","POST","PUT","DELETE","HEAD","OPTIONS"})
                .allowedHeaders("*")
                .maxAge(3600);
    }
}
